<?php

namespace App\Http\Controllers\Admin;

use App\Handlers\ImageUploadHandler;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Category;
class GoodsController extends Controller
{
    public function category(Request $request)
    {
        $list = Category::query()->orderBy('sort','desc')->get()->toArray();
        $temp = $this->getHtmlCate($list,'-');
        $lists = collect($temp);
        return view('admin.category.index',compact('lists'));
    }

    private function getHtmlCate($cate,$html="-",$pid=0, $level=0){
        //定义空数组
        $arr = array();
        //遍历数组
        foreach($cate as $v){
            if($v['pid']        == $pid){
                $v['html']      = str_repeat($html, $level);
                $v['level']     = $level;
                if ($pid ==0){
                    $v['p_name']    = '顶级分类';
                }else{
                    $parent         = Category::find($v['pid']);
                    $v['p_name']    = $parent['cate_name'];
                }
                $arr[]          = $v;
                $arr            = array_merge($arr,$this->getHtmlCate($cate,$html,$v['id'],$level+1));
            }
        }
        return $arr;
    }

    public function create()
    {
        $list = Category::all()->pluck('id','cate_name');
        return view('admin/category/create',compact('list'));
    }

    public function cate_store(Request $request,ImageUploadHandler $uploader,Category $cate)
    {

        $this->validate($request,[
           'cate_name'=>'required|unique:goods_cate|max:10',
            'cate_img'=>'required',
            'pid'=>'required',
            'sort'=>'required',
        ],[
            'cate_name.required'=>'分类名不能为空',
            'cate_name.unique'=>'分类名必须唯一',
            'cate_name.max'=>'分类名长度不能超过10',
            'cate_img.required'=>'分类图片不能为空',
            'pid.required'=>'父级分类不能为空',
            'sort.required'=>'排序不能为空',
        ]);


        $data = $request->except('_token');
        if($request->cate_img){
            $result = $uploader->save($request->cate_img,'banner',1,true);
            if($result){
                $data['cate_img'] = $result['qiniupath'];
                if($request->pid>0){
                    $pid = $request->input('pid');
                    $parent = $cate->find($pid);
                    $data['path'] = $parent->path.'-'.$pid;
                }else{
                    $data['path'] = '0';
                }
            }
        }
        if($cate->storeData($data)){
            return redirect('admin/goods/category')->with('success','新增成功');
        }else{
            return redirect('admin/goods/category')->with('danger','新增失败');
        }

    }

    public function cate_destory(Category $cate,$id)
    {
        $list = $cate->where('pid',$id)->get()->toArray();
        if(!empty($list)){
            return redirect()->back()->with('danger','请先删除下级分类');
        }
        if($id>0 && $cate->destroyData($id)){
            return redirect()->back()->with('success','删除成功');
        }else{
            return redirect()->back()->with('danger','删除失败');
        }
    }

    public function cate_edit(Category $cate,$id)
    {
        $list = Category::all()->pluck('id','cate_name');
        $category = $cate->find($id);
        return view('admin/category/create',compact('category','list'));
    }

    public function cate_update(Category $cate,Request $request,ImageUploadHandler $uploader,$id)
    {
        $request->validate([
            'cate_name'=>'required|max:10',
            'pid'=>'required',
            'sort'=>'required',
        ],[
            'cate_name.required'=>'分类名不能为空',
            'cate_name.unique'=>'分类名必须唯一',
            'cate_name.max'=>'分类名长度不能超过10',
            'pid.required'=>'父级分类不能为空',
            'sort.required'=>'排序不能为空',
        ]);
        $data = $request->except('_token');
        if($request->cate_img){
            $result = $uploader->save($request->cate_img,'banner',1,true);
            if($result){
                $data['cate_img'] = $result['qiniupath'];
            }
        }
        if($request->pid>0){
            $pid = $request->input('pid');
            $parent = $cate->find($pid);
            $data['path'] = $parent->path.'-'.$pid;
        }else{
            $data['path'] = '0';
        }
        if($cate->updateData($data,$id)){
            return redirect('admin/goods/category')->with('success','更新成功');
        }else{
            return redirect('admin/goods/category')->with('danger','更新失败');
        }
    }
}
